
var app = getApp()
var Bmob = require('../../utils/bmob.js');
Page({
  data: {
    userInfo: {
      nickname: '',
      sex: 0,
      cover_thumb: 'http://img.zhichiwangluo.com/zc_app_default_photo.png'
    },
    genderArr: ['男', '女'],
    isFromBack: false,
    phone: '',
    disabled: false,
    check:'否'
  },
  onLoad: function () {
    var userInfo = app.getUserInfo(),
      phone = userInfo.phone,
      data = {
        'userInfo.nickname': userInfo.get('nickName'),
        'userInfo.sex': userInfo.sex,
        'userInfo.cover_thumb': userInfo.get('userPic'),
        'userInfo.phone': userInfo.get('mobilePhoneNumber'),
      };

    if (phone) {
      data.phone = phone;
    }
    this.setData(data);
  },
  onShow: function () {
    if (this.data.isFromBack) {
      var phone = app.getUserInfo().phone;
      if (phone) {
        this.setData({
          phone: phone
        })
      }
    } else {
      this.setData({
        isFromBack: true
      });
    }
  },
  onShareAppMessage: function (e) {
    return app.onPageShareAppMessage(e);
  },
  choosePhoto: function () {
    var that = this;
    app.chooseImage(function (imgUrl) {
      that.setData({
        'userInfo.cover_thumb': imgUrl
      })
    });
  },
  changeGender: function (e) {
    this.setData({
      'userInfo.sex': e.detail.value
    })
  },
  inputNickname: function (e) {
    this.setData({
      'userInfo.nickname': e.detail.value
    })
  },
  saveUserInfo: function (e) {
    var data = this.data.userInfo;
    var that=this;
    app.sendRequest({
      url: '/index.php?r=AppData/saveUserInfo',
      method: 'post',
      data: data,
      success: function (res) {
        if (res.status === 0) {
          app.setUserInfoStorage(data);
         
          
          app.turnBack();
        }
      }
    });
    
    
  },

save_apply:function(e){
var that=this;
    var work = Bmob.Object.extend("franchisee_apply");
    var works = new Bmob.Query(work);
    works.equalTo("openid", app.globalData.userInfo.weixin_id);
    works.find({
      success: function (result) {
        if (result.length) {
          wx.showToast({
            title: '请勿重复申请',
            icon: 'success',
            duration: 2000
          })



        }
        else {
          var Work = Bmob.Object.extend("franchisee_apply");
          var works = new Work();
          if (e.detail.value.work_wx && e.detail.value.work_text) {
            works.set('openid', app.globalData.userInfo.weixin_id);
            works.set('work_phone', that.data.phone);

            works.set('work_wx', e.detail.value.work_wx);
            works.set('work_content', e.detail.value.work_text);
            works.save(null, {
              success: function (result) {
                // 添加成功，返回成功之后的objectId（注意：返回的属性名字是id，不是objectId），你还可以在Bmob的Web管理后台看到对应的数据
                wx.showToast({
                  title: '发布成功',
                  icon: 'success',
                  duration: 2000
                })
              },
              error: function (result, error) {
                // 添加失败
                console.log('创建日记失败');

              }
            });
          }
          else {
            wx.showToast({
              title: '请填写完整',
              icon: 'success',
              duration: 2000
            })
          }
        }

      }
    })
  

},
  bindCellphonePage: function () {
    app.turnToPage('/pages/bindCellphone/bindCellphone');
  },
  radioChange: function (e) {
    var that = this;
   
    if (e.detail.value == '是')
      if (that.data.phone&&e.detail.value == '是')
        that.setData({
          disabled: true,
        
        })
        else
        {
      
      
       wx.showToast({
         title: '请先绑定手机',
         icon: 'success',
         duration: 2000
       })}
    
    if (e.detail.value == '否')
      that.setData({
        disabled: false,
     
      })
    console.log(that.data.disabled);
  }

})




